﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class DailyIntersetCal : Form
    {

        String AgentCode = "";

        public DailyIntersetCal()
        {
            InitializeComponent();
            try
            {
                if (!String.IsNullOrEmpty(DailyInterestServices.GetRecentDate()))
                {
                    PrevDate.Text =
                     DateTime.Parse(DailyInterestServices.GetRecentDate()).ToString("dd/MM/yyyy");
                }
               

                Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
                AgentCode = AgentCodeConfig.Value;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message,"Error");
            }
            
            
        }

        private void DailyIntersetCal_Load(object sender, EventArgs e)
        {

        }

        private void ProcessBtn_Click(object sender, EventArgs e)
        {

            AgentCollection AgentCol = null;
            Dictionary<string, double> ClientList = null;
            //Dictionary<string, double> AgentInterestMap  = new Dictionary<string, double>();
            Dictionary<string, double> BalanceMap = null;
            try
            {
                ClientList = ClientService.GetClientCodeInterestCollection(AgentCode);
                //AgentCol = AgentServices.GetAgentCollection();
                BalanceMap = LoanAccountServices.GetAccBalanceCollection(dateTimePicker1.Value.Date, AgentCode);

                //foreach (Agent NewAgent in AgentCol)
                //{
                //    AgentInterestMap[NewAgent.Code] = NewAgent.Interest_Rate;
                //}

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Database error occurred.", "Error");
                return;
            }

            if (BalanceMap == null || BalanceMap.Count == 0)
            {
                MessageBox.Show("No clients available", "Message");
                return;
            }
            int count = 0;
            progressBar1.Maximum = BalanceMap.Count;
            progressBar1.Value = 0;
            double LoanBalance = 0.0f;
            double InterestRate = 0.0f;
            double Interest = 0.0f;


            foreach (KeyValuePair<string, double> ClientBal in BalanceMap)
            {
                count++;                               
                progressBar1.Increment(1);

                try
                {
                    LoanBalance = ClientBal.Value;
                    if (LoanBalance == 0.0f)
                        continue;
                    InterestRate = ClientList[ClientBal.Key];
                    Interest = LoanBalance * (InterestRate / (100*365));
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Invalid client");
                    continue;
                }                   
                

                DailyInterest DailyInt = new DailyInterest();
                try
                {
                    DailyInt.ClientCode = ClientBal.Key;
                    DailyInt.Interest = Interest;
                    DailyInt.InterestDate = DateTime.Parse(dateTimePicker1.Value.ToShortDateString());
                    DailyInterestServices.Add(DailyInt);
                }
                catch (System.Data.OracleClient.OracleException Ora)
                {
                    if (Ora.Code == 1)
                    {
                        //MessageBox.Show("Code : " + Ora.Code + " , " + Ora.ErrorCode);                        
                        DailyInterestServices.Update(DailyInt);
                    }
                    else
                        MessageBox.Show(Ora.Message, "Error!");
                }
              
            }

        }


        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            if (dateTimePicker1.Value > DateTime.Now)
            {
                dateTimePicker1.Value = DateTime.Now;
                MessageBox.Show("Can not select a future date.", "Error");

            }
        }
    }
}
